#include <unistd.h>
#include "ktota.h"
#include "ktota_slave.h"
#include "simple_uart.h"
#include "crc.h"

#define devname "/dev/ttyUSB0"
#define OTA_BASE 0x08200000

/* 实现接口 */
uint32_t ota_calcrc32 (const uint8_t *data, uint32_t size) {
    return calcrc32(data, size);
}

int32_t ota_flash_erase(void) {

}

void ota_flash_write(uint32_t addr, uint32_t *data, uint32_t len) {
    addr += OTA_BASE;
    printf("write addr %x, %d\r\n", addr, len);

}
void ota_msg_send(uint8_t *buf, uint32_t len) {
    (void)simple_uart_send(buf, 4);
}

int32_t ota_msg_recv(uint8_t *buf, uint32_t len) {
    return simple_uart_rec(buf, len);
}

void msg_handle(uint8_t *buf, uint32_t len) {
    ktota_slave_msg_handle(buf, len);
}

uint8_t rbuf[3000];
int main(void) {
    int32_t rlen = 0;
    if( simple_uart_init(devname, 115200) ) {
        printf("open %s faild\r\n", devname);
        return -1;
    }
    while(1) {
        rlen = ota_msg_recv(rbuf, 2080);
        if(rlen > 0) {
            if(rlen >= 4) {
                msg_handle(rbuf, (uint32_t)rlen);
            }
        }
        usleep(10*1000);
    }

    simple_uart_close();
    printf("close uart\r\n");
    return 0;
}




